package com.xinzhi.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class ConvertUtil {
    //如何把文件上传服务器以及校验java
    //如何找到服务器对应路径
    //doc提取文字
    public static String readDoc(String pathName){
        String text=null;
        try {
            // 读取doc文档
            FileInputStream fis = new FileInputStream(pathName);
            HWPFDocument doc = new HWPFDocument(fis);

            // 提取文本
            WordExtractor extractor = new WordExtractor(doc);
            text = extractor.getText();

            // 输出文本
            System.out.println(text);

            // 关闭输入流
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;

    }
    //docx提取文字
    public static String readDocx(String pathName) {
        String file1="";

        try (FileInputStream fis = new FileInputStream(pathName);
             XWPFDocument document = new XWPFDocument(fis))
        {
            for (XWPFParagraph para : document.getParagraphs()) {
                file1+=para.getText();
                System.out.println(para.getText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file1;

    }

    //pdf提取文字
    public static String readPdf(String pathName) {
        String text = "";
        try {
            PDDocument document = PDDocument.load(new File(pathName));
            PDFTextStripper pdfStripper = new PDFTextStripper();
            text = pdfStripper.getText(document);
            System.out.println(text);
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }
}
